//
//  GWTopicPictureView.m
//  百思不得姐
//
//  Created by qianfeng on 16/3/5.
//  Copyright © 2016年 高炜. All rights reserved.
//

#import "GWTopicPictureView.h"
#import "GWTopicModel.h"
#import <UIImageView+WebCache.h>
#import <SVProgressHUD.h>
#import "GWMyCircularProgressView.h"
@interface GWTopicPictureView ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollVIew;
@property (weak, nonatomic) IBOutlet GWMyCircularProgressView *progressView;

@property (weak, nonatomic)UIImageView *imageView;
@end

@implementation GWTopicPictureView
- (IBAction)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)save {
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), 0);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (self.imageView.image ==nil) {
        [SVProgressHUD showErrorWithStatus:@"图片还未下载完毕~~"];
    }
    if (!error) {
        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:@"保存失败"];
    }
}

-(void)setTopic:(GWTopicModel *)topic
{
    _topic =topic;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:topic.largeImage] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        self.progressView.hidden =NO;
        [self.progressView setProgress:self.topic.progress animated:NO];
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.progressView.hidden =YES;
    }];
    CGFloat pictureH = GWScreenW /topic.width *topic.height;
    if (pictureH>GWScreenH) {
        self.imageView.frame =CGRectMake(0, 0, GWScreenW, pictureH);
        self.scrollVIew.contentSize =CGSizeMake(0, pictureH);
    }
    else
    {
        self.imageView.size =CGSizeMake(GWScreenW, pictureH);
        self.imageView.center =CGPointMake(GWScreenW*0.5, GWScreenH*0.5);
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
      UIImageView *imageView =[[UIImageView alloc]init];
    self.imageView =imageView;
    [self.scrollVIew addSubview:imageView];
    [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(back)]];
    imageView.userInteractionEnabled =YES;
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
